package com.cloud.pole.shop.controller.goods.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.shop.biz.goods.ShopGoodsBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsPromotionBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoods;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsPromotion;
import com.cloud.pole.shop.model.entity.shops.ShopShopsComponent;
import com.cloud.pole.shop.model.vo.goods.ShopGoodsPromotionAdminVo;
import com.cloud.pole.shop.service.goods.GoodsPromotionService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

/**
 * 
 * <p>
 * Title: MenuController
 * </p>
 * <p>
 * Description:
 * </p>
 * 
 * @author hsg
 * @date 2019年2月27日
 */
@Controller
@RequestMapping("/goods/promotion/admin")
@Slf4j
public class GoodsPromotionAdminController extends BaseController<ShopGoodsPromotionBiz, ShopGoodsPromotion> {
	@Autowired
	private GoodsPromotionService promotionService;

	@RequestMapping(value = "/getPageList", method = RequestMethod.GET)
	@ResponseBody
	public ApiRestResponse<ShopGoodsPromotionAdminVo> getPageList(@RequestHeader(defaultValue = "") String token,
			@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,
			@RequestParam(defaultValue = "") String goodsName, @RequestParam(defaultValue = "") String shopsName,
			@RequestParam(defaultValue = "") String promotionType) {
		try {
			goodsName = goodsName.trim();
			shopsName = shopsName.trim();
			Map<String, Object> map = new HashMap<>();
			map.put("goodsName", goodsName);
			map.put("shopsName", shopsName);
			map.put("promotionType", promotionType);
			PageInfo<ShopGoodsPromotionAdminVo> pageInfo = promotionService
					.selectShopGoodsPromotionAdminVoListByMap(token, map, page, limit);
			return ApiRestResponse.successOfPage(CommonConstants.STATUS_OK_MESSAGE, (int) pageInfo.getTotal(),
					pageInfo.getPages(), pageInfo.getPageSize(), pageInfo.getList());
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}

	@RequestMapping(value = "/deletePromotionBatch", method = RequestMethod.DELETE)
	@ResponseBody
	public ApiRestResponse<Boolean> deletePromotionBatch(@RequestHeader(defaultValue = "") String token,
			@RequestParam(defaultValue = "") String promotionIds) {
		try {
			log.info("promotionIds[" + promotionIds + "]");
			ParamException.err(StringUtils.isBlank(promotionIds), "promotionIds不能为空");
			promotionService.deletePromotionBatch(token, promotionIds);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, true);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}

	@RequestMapping(value = "/deletePromotion/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public ApiRestResponse<Boolean> deletePromotion(@RequestHeader(defaultValue = "") String token,
			@PathVariable int id) {
		try {
			promotionService.deletePromotion(token, id);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, true);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}

	@RequestMapping(value = "/addPromotion", method = RequestMethod.POST)
	@ResponseBody
	public ApiRestResponse<Boolean> addPromotion(@RequestHeader(defaultValue = "") String token,
			@RequestBody ShopGoodsPromotion entity) {
		try {
			promotionService.addPromotion(token, entity);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, true);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}

	@RequestMapping(value = "/modifyPromotion", method = RequestMethod.PUT)
	@ResponseBody
	public ApiRestResponse<Boolean> modifyPromotion(@RequestHeader(defaultValue = "") String token,
			@RequestBody ShopGoodsPromotion entity) {
		try {
			promotionService.modifyPromotion(token, entity);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, true);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/promotionList")
	@ResponseBody
	public ApiRestResponse<ShopGoodsPromotion> list(Integer pageNo, Integer pageSize, Integer shopId,
			Integer componentId) {
		pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
		pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
		try {
			PageInfo<ShopGoodsPromotion> pageInfo = promotionService.selectPageShopAdminList(shopId, componentId,
					pageNo, pageSize);
			return ApiRestResponse.successOfPage("成功", (int) pageInfo.getTotal(), pageInfo.getPages(),
					pageInfo.getPageSize(), pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@GetMapping("promotionTermLike")
	@ResponseBody
	public ApiRestResponse<ShopGoodsPromotion> getShopGoodsPromotionById(Integer shopId, Integer indexRecommNum,
			Integer promotionId) {
		try {
			ShopGoodsPromotion selectById = promotionService.selectById(shopId, indexRecommNum, promotionId);
			return ApiRestResponse.success("200", selectById);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	}
	
	@PutMapping("updatePromotion")
	@ResponseBody
	public ApiRestResponse<ShopGoodsPromotion> updatePromotionCon(@RequestBody ShopGoodsPromotion shopGoodsPromotion){
		try {
			promotionService.updateShopGoodsPromotion(shopGoodsPromotion);
			return ApiRestResponse.success("200", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	}
}
